<!--
 * @Description: Overlay 编排
 * @Author: 0001100986/LiuShuai
 * @Date: 2021-06-10 14:13:33
 * @LastEditTime: 2021-10-22 18:02:14
 * @LastEditors: 0001100986/LiuShuai
-->
<template>
  <div class="page-content">
    <template v-if="show === 0">
      <table-bar :showTop="false" :columns="columns" @changeColumn="changeColumn">
        <div slot="top">
          <el-form :model="filterSearch" label-width="80px">
            <el-row :gutter="20">
              <el-col :xs="24" :sm="12" :lg="6">
                <el-form-item label="编排名称">
                  <el-input v-model.trim="filterSearch.keyword" placeholder="请输入名称"></el-input>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :lg="6">
                <el-form-item label="编排类型">
                  <el-select
                    v-model="filterSearch.type"
                    style="width:100%;"
                    placeholder="请选择类型"
                    v-adaption
                  >
                    <el-option label="HubSpoke" value="Hub"></el-option>
                    <el-option label="FullMesh" value="full"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <!-- 搜索 重置 -->
              <el-row :xs="24" :sm="12" :lg="6" style="float: right; margin-right: 10px;">
                <el-button type="primary" @click="search">搜索</el-button>
                <el-button @click="reset">重置</el-button>
              </el-row>
            </el-row>
          </el-form>
        </div>
        <div slot="bottom">
          <!-- 新增 -->
          <el-button size="medium" type="primary" plain @click="showCreate" v-power="2">新建</el-button>
        </div>
      </table-bar>
      <tao-table
        class="table-box"
        :data="dataList"
        :page="page"
        @changePage="changePage"
        :showPage="true"
        ref="table"
      >
        <!-- 详情 -->
        <el-table-column align="center" width="80" label="详情" type="expand">
          <template slot-scope="scope">
            <info :row="scope.row" />
          </template>
        </el-table-column>
        <el-table-column
          label="ID"
          align="center"
          width="100"
          prop="id"
          sortable
          v-if="columns[0].show"
        />
        <el-table-column
          label="名称"
          min-width="200"
          show-overflow-tooltip
          prop="userlabel"
          v-if="columns[1].show"
        >
          <template slot-scope="scope">
            <span class="overlay-title" @click="overlayInfo(scope.row)">{{scope.row.userlabel}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="类型"
          align="center"
          width="150"
          sortable
          prop="type"
          v-if="columns[2].show"
        >
          <template slot-scope="scope">{{scope.row.type === 'Hub' ? 'HubSpoke' : 'FullMesh'}}</template>
        </el-table-column>
        <el-table-column
          label="租户"
          align="center"
          width="150"
          show-overflow-tooltip
          prop="dgName"
          v-if="columns[3].show"
        />
        <el-table-column
          label="VPN"
          align="center"
          width="150"
          show-overflow-tooltip
          prop="vpnName"
          v-if="columns[4].show"
        />
        <!-- 操作 编辑 删除-->
        <el-table-column fixed="right" align="center" :label="$t('user.Operation')" width="150px">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope)"
              v-power="scope.row.dgId === 100000 ? 0 : 2"
            >编辑</el-button>
            <el-button
              type="text"
              style="color: #FA6962"
              icon="el-icon-delete"
              @click="handleDelete(scope)"
              v-power="scope.row.dgId === 100000 ? 0 : 2"
            >删除</el-button>
          </template>
        </el-table-column>
      </tao-table>
    </template>
    <template v-if="show === 1">
      <create :tenant="tenant" @handleChange="handleChange($event)" />
    </template>
    <template v-if="show === 2">
      <update
        :overlay="overlay"
        :tenant="tenant"
        :subls="subls"
        :sitels="sitels"
        :vpnls="vpnls"
        @handleChange="handleChange($event)"
      />
    </template>
    <topology
      v-if="dialogStatus"
      :dialogStatus="dialogStatus"
      :overlay="overlay"
      @handleDialog="handleDialog($event)"
    />
  </div>
</template>

<script>
import create from './create';
import update from './update';
import info from './info';
import topology from './topology';
export default {
  components: {
    create,
    update,
    info,
    topology
  },
  data () {
    return {
      dataList: [],
      filterSearch: {
        keyword: '',
        type: ''
      },
      page: {
        page: 1,
        pageSize: 15,
        total: 0
      },
      columns: [
        { name: 'ID', show: true },
        { name: '名称', show: true },
        { name: '类型', show: true },
        { name: '租户', show: true },
        { name: 'VPN', show: true }
      ],
      show: 0,
      tenant: [],
      overlay: {},
      subls: [],
      sitels: [],
      vpnls: [],
      dialogStatus: false
    };
  },
  mounted () {
    let _this = this;
    _this.$nextTick(() => {
      _this.refresh();
    });
  },
  methods: {
    // 展示拓扑
    overlayInfo (data) {
      let _this = this;
      _this.dialogStatus = true;
      _this.overlay = data;
    },
    // 回调
    handleDialog (val) {
      let _this = this;
      _this.dialogStatus = val;
    },
    // 编辑Overlay
    handleUpdate (scope) {
      let _this = this;
      // 获取当前登录用户
      let loginUser = _this.$store.state.user.user.info;
      let params = {
        limit: 0,
        offset: 0,
        hasDefault: loginUser.dgId === 100000 ? true : false
      };
      _this.$http.post({
        url: '/api/sdwan/tenant/getMultiDg',
        data: params
      }).then(res => {
        let { status, resultBody } = res;
        if (status === '0') {
          _this.tenant = resultBody.objects;
          _this.overlay = scope.row;
          _this.show = 2;
        }
      });
    },
    // 新建Overlay
    showCreate () {
      let _this = this;
      // 获取当前登录用户
      let loginUser = _this.$store.state.user.user.info;
      let params = {
        limit: 0,
        offset: 0,
        hasDefault: loginUser.dgId === 100000 ? true : false
      };
      this.$http.post({
        url: '/api/sdwan/tenant/getMultiDg',
        data: params
      }).then(res => {
        let { status, resultBody } = res;
        if (status === '0') {
          _this.tenant = resultBody.objects;
          _this.show = 1;
        }
      });
    },
    // 回调
    handleChange (val) {
      let _this = this;
      _this.show = val;
    },
    // 搜索
    search () {
      let _this = this;
      _this.page.page = 1;
      _this.refresh();
    },
    // 重置
    reset () {
      let _this = this;
      _this.page.page = 1;
      _this.filterSearch = {
        keyword: null,
        type: null
      };
      _this.refresh();
    },
    // 刷新
    refresh () {
      let _this = this;
      let params = {
        keyword: _this.filterSearch.keyword,
        type: _this.filterSearch.type,
        limit: _this.page.pageSize,
        offset: (_this.page.page - 1) * _this.page.pageSize
      };
      _this.$http.post({
        url: '/api/sdwan/overlay/query',
        data: params
      }).then(res => {
        if (res.status === '0') {
          let { count, objects } = res.resultBody;
          _this.page.total = count;
          _this.dataList = objects;
        }
      });
    },
    // 过滤显示字段
    changeColumn (columns) {
      this.columns = columns;
      this.$refs.table.doLayout();
    },
    // 切换页码 & 显示条数
    changePage (page) {
      if (page.page) {
        this.page.page = page.page;
      } else {
        this.page.page = 1;
        this.page.pageSize = page.limit;
      }
      this.refresh();
    },
    // 删除网元
    handleDelete (scope) {
      let _this = this;
      this.$confirm('您确定要删除当前数据吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        _this.$http.deleted({
          url: '/api/sdwan/overlay/remove/' + scope.row.id
        }).then(res => {
          let { status } = res;
          if (status === '0') {
            _this.$message.success('删除成功！');
            _this.refresh();
          }
        });
      }).catch(() => {
        _this.$message.info('取消删除');
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.overlay-title {
  cursor: pointer;
  color: #3296fa !important;
  &:hover {
    text-decoration: underline;
  }
}
.table-box {
  ::v-deep .el-button {
    padding: 0;
  }
}
</style>